package com.lanou3g.dllo.baidumusic.musiclibrary.radio.radiodetail;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseActivity;
import com.lanou3g.dllo.baidumusic.event.SongIdEvent;
import com.lanou3g.dllo.baidumusic.musiclibrary.radio.RadioDetailActivity;
import com.lanou3g.dllo.baidumusic.tools.MyVolley;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by dllo on 16/7/2.
 */
public class EachRadioDetailActivity extends BaseActivity{

    private EachRadioDetailAdapter adapter;
    private TextView eachRadioType;
    private ViewPager viewPager;
    private ImageView eachRadioReturn;
    private EachRadioDetailBean eachRadioDetailBean;

    @Override
    public int setLayout() {
        return R.layout.activity_eachradio;
    }

    @Override
    public void initView() {
        eachRadioDetailBean = new EachRadioDetailBean();
        eachRadioType = (TextView) findViewById(R.id.eachradio_type);
        eachRadioReturn = (ImageView) findViewById(R.id.eachradio_return);
        adapter = new EachRadioDetailAdapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.eachradio_viewpager);
        viewPager.setAdapter(adapter);
        // 返回键
        eachRadioReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {

        String sceneId =getIntent().getStringExtra("sceneId");
        String sceneName = getIntent().getStringExtra("sceneName");
        eachRadioType.setText(sceneName);
        eachRadioType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EachRadioDetailActivity.this, RadioDetailActivity.class);
                startActivity(intent);
            }
        });

        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.getSmartSongList&page_no=1&page_size=50&scene_id"+ sceneId + "&item_id=0&version=5.2.5&from=ios&channel=appstore";
        MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<EachRadioDetailBean>() {
            @Override
            public void getGsonListener(EachRadioDetailBean data) {
                eachRadioDetailBean = data;
                EventBus.getDefault().post(new SongIdEvent(eachRadioDetailBean.getResult().getSonglist().get(0).getSong_id()));

            }
        },EachRadioDetailBean.class);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 滑动切歌
                EventBus.getDefault().post(new SongIdEvent(eachRadioDetailBean.getResult().getSonglist().get(position).getSong_id()));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
